﻿using System;
using System.Windows;
using System.Windows.Media;
using SegmentDisplay.Common.Digit14;
using SegmentDisplay.Common;

namespace SegmentDisplay.Custom.Date
{
    /// <summary>
    /// Control IndicatorDate
    /// </summary>
    public class IndicatorDate : SegmentBase
    {
        #region Style properties

        /// <summary>
        /// Gets or sets digit style.
        /// </summary>
        public Style DigitStyle
        {
            get { return (Style)GetValue(DigitStyleProperty); }
            set { SetValue(DigitStyleProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the digit style.
        /// </summary>
        public static DependencyProperty DigitStyleProperty =
            DependencyProperty.Register("DigitStyle", typeof(Style), typeof(IndicatorDate));

        /// <summary>
        /// Gets or sets separator style.
        /// </summary>
        public Style SeparatorStyle
        {
            get { return (Style)GetValue(SeparatorStyleProperty); }
            set { SetValue(SeparatorStyleProperty, value); }
        }
        /// <summary>
        /// The dependency property that gets or sets the separator style.
        /// </summary>
        public static DependencyProperty SeparatorStyleProperty =
            DependencyProperty.Register("SeparatorStyle", typeof(Style), typeof(IndicatorDate));

        #endregion

        #region Input values

        /// <summary>
        /// Input value 
        /// </summary>
        public DateTime Value
        {
            get { return (DateTime)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        /// <summary>
        /// The dependency property that gets or sets the Value.
        /// </summary>
        public static DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(DateTime), typeof(IndicatorDate), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(ValueChanged)));

        /// <summary>
        /// Function on change Value property 
        /// </summary>
        /// <param name="d"></param>
        /// <param name="e"></param>
        private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            IndicatorDate sh = (IndicatorDate)d;
            DateTime _currentValue = DateTime.Now;
            if (e.NewValue != null)
            {
                if (e.NewValue is DateTime)
                {
                    DateTime digit = (DateTime)e.NewValue;
                    _currentValue = digit;
                }
                sh.UpdateSegments(sh.IsSwitchOn, _currentValue);
            }
        }

        /// <summary>
        /// Changing on change a value 
        /// </summary>
        /// <param name="isSwitchOn"></param>
        /// <param name="newValue"></param>
        protected void UpdateSegments(bool isSwitchOn, DateTime newValue)
        {
            if (isSwitchOn)
                SetDigit(newValue);
            else
                SetDigit();
        }

        /// <summary>
        /// Default update with current values
        /// </summary>
        protected void UpdateSegments()
        {
            UpdateSegments(IsSwitchOn, Value);
        }

        /// <summary>
        /// public function for force change the value
        /// </summary>
        /// <param name="digit"></param>
        public void SetDigit(DateTime digit)
        {
            try
            {
                string days = digit.Day.ToString("00");
                string month = digit.Month.ToString("00");
                string years = digit.Year.ToString("0000");
                int d1, d2, m1, m2, y1, y2, y3, y4;

                int.TryParse(days.Substring(0, 1), out d1); D1 = d1.ToString();
                int.TryParse(days.Substring(1, 1), out d2); D2 = d2.ToString();
                int.TryParse(month.Substring(0, 1), out m1); M1 = m1.ToString();
                int.TryParse(month.Substring(1, 1), out m2); M2 = m2.ToString();
                int.TryParse(years.Substring(0, 1), out y1); Y1 = y1.ToString();
                int.TryParse(years.Substring(1, 1), out y2); Y2 = y2.ToString();
                int.TryParse(years.Substring(2, 1), out y3); Y3 = y3.ToString();
                int.TryParse(years.Substring(3, 1), out y4); Y4 = y4.ToString();
                Delimiter = true;
            }
            catch (Exception )
            {
                SetDigit();
            }
        }
        public void SetDigit()
        {
            D1 = D2 = M1 = M2 = Y1 = Y2 = Y3 = Y4 = "Empty";
            Delimiter = false;
        }

        #endregion

        #region Management of serment

        /// <summary>
        /// Override a behavior on changing the IsSwitchOn value
        /// </summary>
        /// <param name="newValue"></param>
        public override void OnIsSwitchOnChanged(bool newValue)
        {
            base.OnIsSwitchOnChanged(newValue);
            UpdateSegments(newValue, Value);
        }

        #endregion

        #region Value

        public string D1
        {
            get { return (string)GetValue(D1Property); }
            set { SetValue(D1Property, value); }
        }
        public static DependencyProperty D1Property = DependencyProperty.Register("D1", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string D2
        {
            get { return (string)GetValue(D2Property); }
            set { SetValue(D2Property, value); }
        }
        public static DependencyProperty D2Property = DependencyProperty.Register("D2", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string M1
        {
            get { return (string)GetValue(M1Property); }
            set { SetValue(M1Property, value); }
        }
        public static DependencyProperty M1Property = DependencyProperty.Register("M1", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string M2
        {
            get { return (string)GetValue(M2Property); }
            set { SetValue(M2Property, value); }
        }
        public static DependencyProperty M2Property = DependencyProperty.Register("M2", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string Y1
        {
            get { return (string)GetValue(Y1Property); }
            set { SetValue(Y1Property, value); }
        }
        public static DependencyProperty Y1Property = DependencyProperty.Register("Y1", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string Y2
        {
            get { return (string)GetValue(Y2Property); }
            set { SetValue(Y2Property, value); }
        }
        public static DependencyProperty Y2Property = DependencyProperty.Register("Y2", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string Y3
        {
            get { return (string)GetValue(Y3Property); }
            set { SetValue(Y3Property, value); }
        }
        public static DependencyProperty Y3Property = DependencyProperty.Register("Y3", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public string Y4
        {
            get { return (string)GetValue(Y4Property); }
            set { SetValue(Y4Property, value); }
        }
        public static DependencyProperty Y4Property = DependencyProperty.Register("Y4", typeof(string), typeof(IndicatorDate), new PropertyMetadata("Empty"));

        public bool Delimiter
        {
            get { return (bool)GetValue(DelimiterProperty); }
            set { SetValue(DelimiterProperty, value); }
        }
        public static DependencyProperty DelimiterProperty = DependencyProperty.Register("Delimiter", typeof(bool), typeof(IndicatorDate), new PropertyMetadata(false));

        
        #endregion

        #region Constructor 

        /// <summary>
        /// Static constructor
        /// </summary>
        static IndicatorDate()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(IndicatorDate), new FrameworkPropertyMetadata(typeof(IndicatorDate)));
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public IndicatorDate()
        {
            DefaultStyleKey = typeof(IndicatorDate);
        }

        #endregion
    }
}
